import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    /**
     * 首页政务服务查询
     */
    async queryService(name) {
      let {user} = Taro.getStorageSync('b2b-wechat@login');
      let selectDistance = Taro.getStorageSync('selectDistance');
      const params = {
        name,
        unionId: user?.unionId || '',
        communityId: selectDistance.id,
      };
      let res = await api.pagesIndexController.getService(params);
      if (res.success) {
        action.commonChange('main.serviceList', res.data?.general);
      }
    },
    /**
     * 首页商城商品查询
     */
    async queryShop(params) {
      let {pageNum, shopList} = getData().main;
      const isLogin = Boolean(Taro.getStorageSync('authInfo:token')); //是否登录
      action.commonChange('main.isLoading', true);
      if (isLogin) {
        //登录状态
        try {
          let res = await api.pagesIndexController.getSearchShopListLogin(params);
          if (res.code == 'K-000000') {
            action.commonChange('main.totalPages', res.context.esGoodsInfoPage.totalPages);
            if (pageNum == 0) {
              action.commonChange('main.shopList', res.context.esGoodsInfoPage.content);
            } else {
              let newList = shopList.concat(res.context.esGoodsInfoPage.content);
              action.commonChange('main.shopList', newList);
            }
          } else {
            Taro.showToast({
              title: res.message,
              icon: 'none',
              duration: 2000,
            });
          }
        } catch (e) {
          Taro.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000,
          });
          return;
        }
      } else {
        //非登录状态
        try {
          let res = await api.pagesIndexController.getShopListLoginOut(params);
          if (res.code == 'K-000000') {
            action.commonChange('main.totalPages', res.context.esGoodsInfoPage.totalPages);
            if (pageNum == 0) {
              action.commonChange('main.shopList', res.context.esGoodsInfoPage.content);
            } else {
              let newList = shopList.concat(res.context.esGoodsInfoPage.content);
              action.commonChange('main.shopList', newList);
            }
          } else {
            Taro.showToast({
              title: res.message,
              icon: 'none',
              duration: 2000,
            });
          }
        } catch (e) {
          Taro.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000,
          });
          return;
        }
      }
      action.commonChange('main.isLoading', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageASearchListMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
